package com.hddx.monitor;

import com.hddx.domain.Employee;
import com.hddx.domain.Users;
import com.hddx.mapper.EmployeeMapper;
import com.hddx.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

public class MyBatisMonitor {
    @Test
    public  void  findEmployeeByID(){
        SqlSession sqlSession=MyBatisUtils.getSqlSession();
        //存在硬编码
//        Employee employee =sqlSession.selectOne("employee.findEmployeeById",1);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = mapper.findEmployeeById(4);
        System.out.println(employee);
        sqlSession.close();
    }
    @Test
    public void  findEmployeeALL(){
        SqlSession sqlSession=MyBatisUtils.getSqlSession();
        // 正确：使用 List<Employee> 接收结果
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> allEmployee = mapper.findEmployeeALL();
        System.out.println(allEmployee);
        sqlSession.close();
    }
    @Test
    public void addEmployee(){
        SqlSession sqlSession =MyBatisUtils.getSqlSession();
        Employee employee =new Employee();
        employee.setName("赵六");
        employee.setAge(26);
        employee.setPosition("员工");
//        int i =sqlSession.insert("employee.addEmployee",employee);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        int i = mapper.addEmployee(employee);
        sqlSession.commit();
        System.out.println("插入了"+i+"行数据");
        sqlSession.close();
    }
    @Test
    public void editEmployeeById(){
        SqlSession sqlSession =MyBatisUtils.getSqlSession();
        Employee employee =new Employee();
        employee.setId(4);
        employee.setName("新赵六");
        employee.setAge(28);
        employee.setPosition("经理");
//        int i =sqlSession.update("employee.editEmployeeById",employee);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        int i = mapper.editEmployeeById(employee);
        sqlSession.commit();
        System.out.println("更新了"+i+"行数据");
        sqlSession.close();
    }
    @Test
    public void deleteEmployeeById(){
        SqlSession sqlSession =MyBatisUtils.getSqlSession();
//        int i =sqlSession.delete("employee.deleteEmployeeById",4);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        int i = mapper.deleteEmployeeById(4);
        sqlSession.commit();
        System.out.println("删除了"+i+"行数据");
        sqlSession.close();
    }
//    public static void main(String[] args) throws IOException {
//        //1.获取SqlSessionFactory对象
//        //
//        SqlSessionFactory build = MyBatisUtils.build;
//        SqlSession sqlSession=build.openSession();
//        //2. 初始化MyBatis数据库，创建SqlSessionFactory类的实例
////        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resoeures);
////
////        SqlSession sqlSession= build.openSession();
//        //3. 创建SqlSession实例对象
//        //executor执行器
////        Users users = new Users();
////        users.setUid(3);
////        users.setUname("赵五");
////        users.setUage(12);
////        更新，添加
////        int i = sqlSession.update("test.editusers", users);
//        //删除
////        int i = sqlSession.delete("test.deleteusers",3);
//        Employee employee = sqlSession.selectOne("test.findById",2);
//        sqlSession.commit();
////        System.out.println("更新"+i+"条数据");
////        System.out.println("删除"+i+"条数据");
//        System.out.println(employee);//修改
//        //4. 执行sql，传入参数，获取结果
//
//        sqlSession.close();
//        //5. 关闭session
//        //yumingjiaIDweiyisuoyin
//    }
}

